package com.ehouse;

import java.util.HashMap;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

import com.ehouse.database.LightTable;
import com.ehouse.fragments.ScheduleIrrigationsFragment;
import com.ehouse.fragments.ScheduleLightsFragment;
import com.ehouse.fragments.ScheduleTemperaturesFragment;

public class ScheduleActivity extends FragmentActivity implements OnTabChangeListener {
	public static final String LOG_TAG = "SCHEDULE_ACTIVITY";
	static TabHost tabHost;
	private int currentTab = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.name_schedules);
		setContentView(R.layout.schedule_main);
		initializeInterface();
		int type = (int)getIntent().getIntExtra("type", 0);
		tabHost.setCurrentTab(type);
    }
	
	 @Override
	public void onResume(){
		 super.onResume();
		 reloadFragments();
	}
    
    private void reloadFragments(){
    	FragmentManager fragmentManager = getSupportFragmentManager();
    	ScheduleTemperaturesFragment templistFragment = (ScheduleTemperaturesFragment)fragmentManager.findFragmentById(R.id.schedule_temperatures_fragment);
        templistFragment.reload();
        ScheduleLightsFragment lightslistFragment = (ScheduleLightsFragment)fragmentManager.findFragmentById(R.id.schedule_lights_fragment);
        lightslistFragment.reload();
        ScheduleIrrigationsFragment irrigationlistFragment = (ScheduleIrrigationsFragment)fragmentManager.findFragmentById(R.id.schedule_irrigations_fragment);
        irrigationlistFragment.reload();
    }
    
    
    public void initializeInterface()
    {

	    String[] switchArray = this.getResources().getStringArray(R.array.lightItems);
	    ContentResolver cr = getContentResolver();
	    HashMap<String, Boolean> map = new HashMap<String, Boolean>();
   		for (String s : switchArray) {
   			map.put(s, true);
   		}
	    LightTable.update(cr, map);
	    
	    
    	tabHost=(TabHost)findViewById(R.id.tabHost);
    	tabHost.setup();
    	
    	//Lights Tab
    	TabSpec lights=tabHost.newTabSpec("LightsTab");
    	lights.setIndicator(getResources().getString(R.string.lights_tab), getResources().getDrawable(R.drawable.ic_tab_lights));
    	lights.setContent(R.id.schedule_lights_fragment);
    	tabHost.addTab(lights);
    	tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED);
    	
    	//Temperatures tab
    	TabSpec spec2=tabHost.newTabSpec("TemperatureTab");
    	spec2.setIndicator(getResources().getString(R.string.temps_tab), getResources().getDrawable(R.drawable.ic_tab_temp));
    	spec2.setContent(R.id.schedule_temperatures_fragment);
    	tabHost.addTab(spec2);
    	tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.RED);
    	
    	//Irrigation tab
    	TabSpec spec3=tabHost.newTabSpec("IrrigationsTab");
    	spec3.setIndicator(getResources().getString(R.string.irigs_tab), getResources().getDrawable(R.drawable.ic_tab_irri));
    	spec3.setContent(R.id.schedule_irrigations_fragment);
    	tabHost.addTab(spec3);
    	tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.RED);
    	
    	tabHost.setOnTabChangedListener(this);
    	
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_schedule, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_schedule_add:
				Intent addActivity = new Intent(this, ScheduleAddActivity.class);
				addActivity.putExtra("type", currentTab);
				startActivityForResult(addActivity, currentTab);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onTabChanged(String arg0) {
		currentTab = tabHost.getCurrentTab();
	}
	
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data){
    	reloadFragments();
    }

}
